$(function () {
    let menu = $(".menu").text();

    if (menu === "1"){
        $(".from-login").css({"display":"none"})
        $(".from-register").css({"display":"block"})
        $(".login").css({"color": "#838383"})
        $(".register").css({"color":"#ffffff"})
    }

    const inputUtil = new Input_util();
    const textUtil = new Text_util();
    let login = $(".login");
    let register = $(".register");
    let fromLogin = $(".from-login");
    let fromRegister = $(".from-register")
    //点击切换
    textUtil.showHide(
        login, register, fromRegister, fromLogin, function () {
            login.css({"color": "#fff"});
            register.css({"color": "#838383"});
        }
        , function () {
            login.css({"color": "#838383"});
            register.css({"color": "#fff"});
        }
    )

    let inputPassword = $(".input-password");
    let textPassword = $(".placeholder-password");
    let hintPassword = $(".hint-password");
    //动画事件
    inputUtil.inputAnimation(inputPassword, textPassword, hintPassword, "请输入密码");

    let textId = $(".placeholder-id");
    let inputId = $(".input-id");
    let hintId = $(".hint-id")
    //动画事件
    inputUtil.inputAnimation(inputId, textId, hintId, "请输入小米ID/手机号/邮箱");

    let inputMail = $(".input-mail");
    let textMail = $(".placeholder-mail");
    let hintMail = $(".hint-mail")
    //动画事件
    inputUtil.inputAnimation(inputMail, textMail, hintMail, "请输入手机号/邮箱");

    let inputCode = $(".input-code")
    let textCode = $(".placeholder-code");
    let hintCode = $(".hint-code")
    //动画事件
    inputUtil.inputAnimation(inputCode, textCode, hintCode, "请输入验证码");

    let inputNewPassword = $(".input-new-password")
    let textNewPassword = $(".placeholder-new-password")
    let hintNewPassword = $(".hint-new-password")
    //动画事件
    inputUtil.inputAnimation(inputNewPassword, textNewPassword, hintNewPassword, "请输入密码")
    inputNewPassword.on("click", function () {
        $(".hint-password-specification").text("")
    });

    let loginUtil = new Login();
    let index = new Index();
    //登陆
    $(".login-button").on("click", function () {
        let inputId = $(".input-id").val();
        let inputPassword = $(".input-password").val();
        if ($(".protocol-input").prop("checked")) {
            loginUtil.landing(inputId, inputPassword).then(function (response) {
                // 在这里获取后端返回的响应数据
                console.log(response)
                if (textUtil.clearSpaces(response) !== "-1") {
                    //跳转主页
                    window.location.href = "/www.xiaomi.com/shop?acccountId="+response
                } else {
                    hintText[0].innerText = "账号或密码错误！"
                }
            }).catch(function (e) {
                console.log(e)
                window.location.href = "/www.xiaomi.com/error"
            })
        } else {
            $('.failed-login').text("请勾选用户协议和隐私政策")
        }

    })

    let captcha = new Captcha();
    let code = null;

    // 发送验证码
    $(".obtain").on("click", function () {
        sendCode($(this));
        // 将当前点击的按钮对象传给 sendCode 函数
    });

    // 接受一个参数 btn，表示当前点击的按钮对象
    function sendCode(btn) {
        let id = $(".input-mail").val();

        if (inputUtil.isPhoneNumber(id) === true) {
            captcha.sendPhoneCaptcha(id).then(function (response) {
                code = response;
            }).catch(function (e) {
                console.log(e)
                window.location.href = "/www.xiaomi.com/error"
            })
        } else if (inputUtil.isEmail(id) === true) {
            captcha.sendMailboxCaptcha(id).then(function (response) {
                if (textUtil.clearSpaces(response) !== "200") {
                    window.location.href = "/www.xiaomi.com/error"
                }
            }).catch(function (e) {
                console.log(e)
                window.location.href = "/www.xiaomi.com/error"
            })
        } else {
            hintMail.text("请输入正确的手机号/邮箱")
        }
        let countDown = 60;
        let originalText = btn.text();

        btn.addClass('disabled').off("click");
        btn.text(countDown + '秒后重新获取');

        let countdownInterval = setInterval(() => {
            countDown--;
            btn.text(countDown + '秒后重新获取');

            if (countDown <= 0) {
                clearInterval(countdownInterval);
                btn.text(originalText).removeClass('disabled');

                btn.on("click", function () {
                    sendCode($(this));
                });
            }
        }, 1000);
    }


    //注册
    $(".register-button").on("click", function () {
        let id = $(".input-mail").val();
        let inputCode = $(".input-code").val();
        let password = $(".input-new-password").val();
        let hintCode = $(".hint-code");
        let hintPassword = $(".hint-password-specification");
        if (!inputUtil.checkPassword(password)) {
            $(".hint-new-password").text("")
            hintPassword.text("密码至少包含一个小写字母、一个大写字母、一个数字、密码长度为 8-20 个字符")
            return;
        }
        if (inputUtil.isPhoneNumber(id) === true) {
            console.log(code)
            console.log(inputCode)
            if (textUtil.clearSpaces(code) === textUtil.clearSpaces(inputCode)){
                loginUtil.isPhoneEnroll(id).then(function (response) {
                    if (textUtil.clearSpaces(response) === "false"){
                        let response = loginUtil.phoneRegistration(id,password);
                        if (textUtil.clearSpaces(response) === "true"){
                            location.reload();
                        }else{
                            window.location.href = "/www.xiaomi.com/error"
                        }
                    }else if (textUtil.clearSpaces(response) === "true"){
                        $(".hint-mail").text("账号已存在")
                    }
                }).catch(function (reason) {
                    console.log(reason)
                    window.location.href = "/www.xiaomi.com/error"
                })
            }else{
                hintCode.text("请输入正确验证码")
            }

        } else if (inputUtil.isEmail(id) === true) {
            captcha.verifyMailboxCaptcha(id, code).then(function (response) {
                if (textUtil.clearSpaces(response) === -1) {
                    hintCode.text("请输入正确验证码")
                } else {
                    //判断邮箱是否已经存在
                    loginUtil.isMailboxEnroll(id).then(function (response) {
                        if (textUtil.clearSpaces(response) === "false"){
                            //没有则注册
                            loginUtil.mailboxRegistration(id,password).then(function (response) {
                                if (textUtil.clearSpaces(response) === "true"){
                                    location.reload();
                                } else{
                                    window.location.href = "/www.xiaomi.com/error"
                                }
                            }).catch(function (reason) {

                            })
                        }else if (textUtil.clearSpaces(response) === "true"){
                            //存在则提示
                            $(".hint-mail").text("账号已存在")
                        }
                    }).catch(function (reason) {
                        console.log(reason)
                        window.location.href = "/www.xiaomi.com/error"
                    })
                }
            }).catch(function (reason) {
                console.log(reason)
                window.location.href = "/www.xiaomi.com/error"
            })
        } else {
            hintMail.text("请输入正确的手机号/邮箱")
        }
    })
});